package dao

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

type GrpcRule struct {
	Id             int    `json:"id" gorm:"primary_key" description:"自增主键"`
	ServiceId      int    `json:"service_id" gorm:"column:service_id" description:"服务Id"`
	Port           int    `json:"port" gorm:"column:port" description:"端口"`
	HeaderTransfor string `json:"header_transfor" gorm:"column:header_transfor" description:"header转换支持增加(add)、删除(del)、修改(edit) 格式：add headname headvalue"`
}

func (g *GrpcRule) TableName() string {
	return "gateway_service_grpc_rule"
}

func (g *GrpcRule) Find(c *gin.Context, tx *gorm.DB, search *GrpcRule) (*GrpcRule, error) {
	grpcRule := &GrpcRule{}
	// 根据结构体查询
	err := tx.WithContext(c).Where(search).First(grpcRule).Error
	return grpcRule, err
}

func (g *GrpcRule) Save(c *gin.Context, tx *gorm.DB) error {
	err := tx.WithContext(c).Save(g).Error
	return err
}
